from sklearn.linear_model import Ridge,Lasso
from sklearn.grid_search import GridSearchCV
from sklearn.metrics import mean_squared_error
from sklearn.preprocessing import PolynomialFeatures
from sklearn.cross_validation import KFold,train_test_split
import numpy as np
ridge_alpha=0
def BuildRidgeModel(x,y):
    kfold = KFold(y.shape[0],5)
    alpha_range = np.linspace(0.0001,0.001,1)
    grid_param = {"alpha":alpha_range}
    model = Ridge(normalize=True)
    grid = GridSearchCV(estimator=model,param_grid=grid_param,cv=kfold,\
                        scoring='mean_squared_error')
    grid.fit(x,y)
    global ridge_alpha
    ridge_alpha=grid.best_params_['alpha']
    return grid.best_estimator_
lasso_alpha=0
def BuildLassoModel(x,y):
    kfold = KFold(y.shape[0],5)
    model = Lasso(normalize=True)
    alpha_range = np.linspace(0.0001,0.001,1)
    grid_param = {"alpha":alpha_range}
    grid = GridSearchCV(estimator=model,param_grid=grid_param,cv=kfold,\
                        scoring='mean_squared_error')
    grid.fit(x,y)
    global lasso_alpha
    lasso_alpha= grid.best_params_['alpha']
    return grid.best_estimator_
def model_worth(true_y,predicted_y):
    return np.sqrt(mean_squared_error(true_y,predicted_y))
